<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="../css/bootstrap.min.css">
  <!-- <link rel="stylesheet" href="../css/remove.css"> -->
</head>

<body>
  <div class="container">
    <h1>王者荣耀</h1>

    <a type="button" class="btn btn-success" href="./index.html">添加</a>

    <form>
      <legend>添加英雄</legend>
      <div class="form-group">
        <label for="">name</label>
        <input type="text" class="form-control" id="name" placeholder="Input field">
      </div>
      <div class="form-group">
        <label for="">age</label>
        <input type="text" class="form-control" id="age" placeholder="Input field">
      </div>
      <button type="button" class="btn btn-primary" id='addBtn'>确定</button>
    </form>

  </div>
  <script src="../js/jquery.min.js"></script>
  <script>
    $(() => {
      $('#addBtn').click(() => {
        let data = {
          name: $('#name').val(),
          age: $('#age').val()
        }
        $.ajax({
          type: 'post',
          url: 'http://127.0.0.1:8080/heors/add',
          data,
          success(res) {
            const { message, status } = res;
            if (status != -200) {
              alert('添加成功');
            } else {
              alert('添加失败,已有此英雄');
            }
            location.href = './index.html';
          }
        })
      })
    })
  </script>
</body>

</html>